package encaps.utils.rendering;

import encaps.action.Action;
import encaps.action.AdvancedMovieSearchAction;
import encaps.action.AdvancedPersonSearchAction;
import encaps.action.BasicSearchAction;
import encaps.resources.Config;
import encaps.resources.Image;
import encaps.resources.Labels;
import static encaps.utils.html.HTML.*;
import encaps.utils.html.HTMLElement;
import encaps.utils.html.HTMLElementSequence;
import encaps.utils.html.Styles;

public class PageHeaderBuilder {

    public static final String ADVANCED_SEARCH_PATTERN = "%s?%s=%s&%s=true";
    public static final String ADVANCED_SEARCH_MENU = "advancedSearchMenu";
    public static final String CLOSE_ADVANCED_SEARCH_MENU_FUNCTION = "closeAdvancedSearchMenu()";
    public static final String SHOW_ADVANCED_SEARCH_MENU_FUNCTION = "showAdvancedSearchMenu()";

    private PageHeaderBuilder() {
    }

    public static String createPageHeader() {
        HTMLElementSequence result = new HTMLElementSequence();
        HTMLElement headerText = new HTMLElement(SPAN);
        headerText.addAttribute(CLASS, Styles.PAGE_HEADER_TEXT);
        headerText.addContent(Labels.getLabel(Labels.PAGE_HEADER));
        result.addContent(headerText);
        result.addContent(HTMLElement.createBr());
        HTMLElement form = new HTMLElement(FORM);

        HTMLElement searchField = HTMLElement.createTextField(
                BasicSearchAction.SEARCH_PARAMETER, DEFAULT_INPUT_SIZE);
        form.addContent(searchField);
        form.addContent(" &nbsp; ");

        HTMLElement searchButton = HTMLElement.createImageButton(Action.ACTION, BasicSearchAction.BASIC_SEARCH, Image.getImage(Image.SEARCH));
        //HTMLElement searchButton = HTMLElement.createButton(Action.ACTION, BasicSearchAction.BASIC_SEARCH, Labels.getLabel(Labels.SEARCH_BUTTON));
        form.addContent(searchButton);
        form.addContent(" &nbsp; &nbsp; &nbsp; ");

        HTMLElement movieOption = HTMLElement.createRadioButton(
                BasicSearchAction.ENTITY_TYPE, BasicSearchAction.MOVIE,
                Labels.getLabel(Labels.MOVIE_SEARCH));
        movieOption.addAttribute(CHECKED, "true");
        form.addContent(movieOption);
        form.addContent(" &nbsp; ");

        HTMLElement personOption = HTMLElement.createRadioButton(
                BasicSearchAction.ENTITY_TYPE, BasicSearchAction.PERSON,
                Labels.getLabel(Labels.PERSON_SEARCH));
        form.addContent(personOption);
        form.addContent(" &nbsp; ");

        HTMLElement userOption = HTMLElement.createRadioButton(
                BasicSearchAction.ENTITY_TYPE, BasicSearchAction.USER,
                Labels.getLabel(Labels.USER_SEARCH));
        form.addContent(userOption);


        form.addContent(" &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ");
        //Создание меню расширенного поиска
        HTMLElement advSearch = new HTMLElement(SPAN);
        advSearch.addAttribute(CLASS, Styles.ADVANCED_SEARCH);
        advSearch.addAttribute(ON_MOUSE_OVER, SHOW_ADVANCED_SEARCH_MENU_FUNCTION);
        advSearch.addAttribute(ON_MOUSE_OUT, CLOSE_ADVANCED_SEARCH_MENU_FUNCTION);

        advSearch.addContent(Labels.getLabel(Labels.ADVANCED_SEARCH));

        form.addContent(advSearch);
        HTMLElement menuItems = new HTMLElement(DIV);
        menuItems.addAttribute(ID, ADVANCED_SEARCH_MENU);
        menuItems.addAttribute(CLASS, Styles.ADVANCED_SEARCH_MENU);
        menuItems.addAttribute(ON_MOUSE_OUT, CLOSE_ADVANCED_SEARCH_MENU_FUNCTION);

        HTMLElement movieAdvancedSearch = new HTMLElement(A);
        String movieAdvancedSearchLink = String.format(ADVANCED_SEARCH_PATTERN,
                Config.getHomePage(), Action.ACTION, AdvancedMovieSearchAction.ADVANCED_MOVIE_SEARCH,
                AdvancedMovieSearchAction.SHOW_SEARCH_FORM_ONLY);
        movieAdvancedSearch.addAttribute(HREF, movieAdvancedSearchLink);
        movieAdvancedSearch.addContent(Labels.getLabel(Labels.MOVIE_SEARCH));
        menuItems.addContent(movieAdvancedSearch);

        menuItems.addContent(HTMLElement.createBr());

        HTMLElement personAdvancedSearch = new HTMLElement(A);
        String personAdvancedSearchLink = String.format(ADVANCED_SEARCH_PATTERN,
                Config.getHomePage(), Action.ACTION, AdvancedPersonSearchAction.ADVANCED_PERSON_SEARCH,
                AdvancedPersonSearchAction.SHOW_SEARCH_FORM_ONLY);
        personAdvancedSearch.addAttribute(HREF, personAdvancedSearchLink);
        personAdvancedSearch.addContent(Labels.getLabel(Labels.PERSON_SEARCH));
        menuItems.addContent(personAdvancedSearch);

        advSearch.addContent(menuItems);

        result.addContent(form);

        return result.toString();
    }
}
